package com.myapplication.ui.homefragment.shopfragment;


import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ScrollView;

import com.bumptech.glide.Glide;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshScrollView;
import com.myapplication.R;
import com.myapplication.bean.Banner;
import com.myapplication.bean.Shop;
import com.myapplication.config.JUrlConfig;
import com.myapplication.customview.MyGirdView;
import com.myapplication.ui.homefragment.shopfragment.activity_banner.activity_banner1.Activity_Banner1;
import com.myapplication.ui.homefragment.shopfragment.activity_banner.activity_banner2.Activity_Banner2;
import com.myapplication.ui.homefragment.shopfragment.activity_banner.activity_banner3.Activity_Banner3;
import com.myapplication.ui.homefragment.shopfragment.activity_banner.activity_banner4.Activity_Banner4;
import com.myapplication.ui.homefragment.shopfragment.activity_shop_detail.Activity_Shop_Detail;
import com.myapplication.utils.Contants;
import com.youth.banner.BannerConfig;
import com.youth.banner.Transformer;
import com.youth.banner.listener.OnBannerClickListener;
import com.youth.banner.loader.ImageLoader;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;


/**
 * Created by zitaojiang on 2016/10/8.
 */
public class Fragment_Shop extends Fragment implements Contract_Shop.view {


    @Bind(R.id.banner)
    com.youth.banner.Banner banner;
    @Bind(R.id.gridlview)
    MyGirdView gridlview;
    @Bind(R.id.pullscrollview)
    PullToRefreshScrollView pullscrollview;
    private Contract_Shop.Presenter mPresenter;
    Map<String, String> params = new HashMap<>();
    private ShopFragmentAdapter adapter;
    private List<Shop.RowsBean> list = new ArrayList<>();
    private int page = Contants.PAGE_NUM;
    private List<Banner.AdListBean> banner_list;
    private List<String> bannerImgUrl = new ArrayList<>();


    @Nullable
    @Override

    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view_Shop = inflater.inflate(R.layout.fragment_shop, null);
        ButterKnife.bind(this, view_Shop);
        adapter = new ShopFragmentAdapter(getActivity(), list);
        mPresenter = new Presenter_Shop(this);
        mPresenter.putData();
        initRefreshParams();
        initRefresh();
        initWebView();
        return view_Shop;
    }

    private void initWebView() {
        gridlview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent = new Intent(getActivity(), Activity_Shop_Detail.class);
                intent.putExtra("SpreadUrl", list.get(i).getProductUrl());
                intent.putExtra("Name", list.get(i).getName());
                startActivity(intent);

            }
        });
    }

    private void initRefreshParams() {

        params.put(JUrlConfig.key.ACT, JUrlConfig.DefaultValue.ACT);
        params.put(JUrlConfig.key.V, JUrlConfig.DefaultValue.V);
        params.put(JUrlConfig.key.PAGES, String.valueOf(page));
        params.put(JUrlConfig.key.BC, JUrlConfig.DefaultValue.BC);
        params.put(JUrlConfig.key.SC, JUrlConfig.DefaultValue.SC);
        params.put(JUrlConfig.key.SORTS, JUrlConfig.DefaultValue.SORTS);
        params.put(JUrlConfig.key.CHANNEL, JUrlConfig.DefaultValue.CHANNEL);
        params.put(JUrlConfig.key.CKEY, JUrlConfig.DefaultValue.CKEY);
        params.put(JUrlConfig.key.DAYNEWS, JUrlConfig.DefaultValue.DAYNEWS);
        params.put(JUrlConfig.key.LPRICE, JUrlConfig.DefaultValue.LPRICE);
        params.put(JUrlConfig.key.HPRICE, JUrlConfig.DefaultValue.HPRICE);
        params.put(JUrlConfig.key.TBCLASS, JUrlConfig.DefaultValue.TBCLASS);
        params.put(JUrlConfig.key.ACTID, JUrlConfig.DefaultValue.ACTID);
        params.put(JUrlConfig.key.BRANDID, JUrlConfig.DefaultValue.BRANDID);
        params.put(JUrlConfig.key.PREDITE, JUrlConfig.DefaultValue.PREDITE);
        params.put(JUrlConfig.key.INDEX, JUrlConfig.DefaultValue.INDEX);
        mPresenter.putData(params);

    }

    private void initRefresh() {

        pullscrollview.setMode(PullToRefreshBase.Mode.BOTH);
        pullscrollview.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ScrollView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ScrollView> refreshView) {
                page = 1;
                initRefreshParams();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ScrollView> refreshView) {
                page++;
                initRefreshParams();
            }
        });


    }

    private void initBanner() {



        banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR_TITLE);
        banner.setDelayTime(3000);
        banner.setBannerAnimation(Transformer.DepthPage);
        banner.setImageLoader(new ImageLoader() {
            @Override
            public void displayImage(Context context, Object path, ImageView imageView) {
                Glide.with(context).load(path).into(imageView);
            }
        });
        banner.setImages(bannerImgUrl);

        banner.start();
        bannerImgUrl.clear();
        //设置banner的点击事件
        banner.setOnBannerClickListener(new OnBannerClickListener() {
            @Override
            public void OnBannerClick(int position) {
                switch (position){

                    case 1:
                        Intent intent_One=new Intent(getActivity(),Activity_Banner1.class);
                        intent_One.putExtra("imgUrl1",banner_list.get(0).getImgUrl());
                        startActivity(intent_One);
                        break;
                    case 2:
                        Intent intent_Two=new Intent(getActivity(), Activity_Banner2.class);
                        intent_Two.putExtra("imgUrl2",banner_list.get(1).getImgUrl());
                        startActivity(intent_Two);
                        break;
                    case 3:
                        Intent intent_Three=new Intent(getActivity(), Activity_Banner3.class);
                        intent_Three.putExtra("imgUrl3",banner_list.get(2).getImgUrl());
                        startActivity(intent_Three);
                        break;
                    case 4:
                        Intent intent_Four=new Intent(getActivity(), Activity_Banner4.class);
                        intent_Four.putExtra("imgUrl4",banner_list.get(3).getImgUrl());
                        startActivity(intent_Four);
                        break;
                }
            }
        });


    }

    @Override
    public void onGetBannerSuccess(List<Banner.AdListBean> bannerbean) {
        for (Banner.AdListBean adListBean : bannerbean) {
            bannerImgUrl.add(adListBean.getImgUrl());
        }
        initBanner();
        banner_list=bannerbean;
    }

    @Override
    public void onGetBannerFail(String error) {

    }

    @Override
    public void onGetDataSuccess(List<Shop.RowsBean> rowsBeen) {
        if (page == 1) {
            list.clear();
            adapter.setList(rowsBeen);
            adapter.notifyDataSetChanged();
            gridlview.setAdapter(adapter);
            list.addAll(rowsBeen);
            pullscrollview.onRefreshComplete();
        } else if (page > 1) {
            list.addAll(rowsBeen);
            adapter.setList(list);
            adapter.notifyDataSetChanged();
            pullscrollview.onRefreshComplete();
        }


    }

    @Override
    public void onGetDataFail(String error) {

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }
}
//Toast.makeText(getActivity(),"下载失败",Toast.LENGTH_LONG).show();
//        Log.i("tag","-----"+t.getMessage());//打印错误日志


//  http://www.syby8.com/apptools/productlist.aspx?
// act=getproductlist&v=34&pages=1&bc=0&sc=0&sorts=&channel=0&ckey=&daynews=&lprice=0&hprice=0&tbclass=0&actid=0&brandid=0&predate=2016-08-28+17%3A43%3A19&index=1
//        params.put("act","getproductlist");
//        params.put("v","34");
//        params.put("pages","1");
//        params.put("bc","0");
//        params.put("sc","0");
//        params.put("sorts","");
//        params.put("channel","0");
//        params.put("ckey","");
//        params.put("daynews","");
//        params.put("lprice","0");
//        params.put("hprice","0");
//        params.put("tbclass","0");
//        params.put("actid","0");
//        params.put("brandid","0");
//        params.put("predate","2016-08-28+17%3A43%3A19");
//        params.put("index","1");
